package shortcircuit.graphics;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import shortcircuit.Display;
import shortcircuit.GameEngine;
import shortcircuit.engine.InstructionsEngine;
import shortcircuit.engine.MainMenu;

/**
 * This class is the instructions screen, and holds instructions for the entire game.
 * It is a screen, which means that it directly extends JPanel and is added directly
 * onto the Display as a JPanel. It simply holds MouseInputs to translate user input and
 * progresses through a series of pictures designed to give the user instructional
 * information on how to use the game.
 * @author David Li
 *
 */

public class InstructionsScreen extends JPanel
{
	private Image displayedImage;
	private Image bgImage;
	private Image backImage, nextImage;
	private Image offImage;
	private Graphics offGraphics;
	private int progressCount;
	private Image[] displayableImages;
	private JButton backButton, nextButton;
	private GameEngine _Engine;
	public boolean powerUpSelected;

	public InstructionsScreen(GameEngine theEngine)
	{
		powerUpSelected = false;
		this._Engine = theEngine;
		displayableImages = new Image[6];
		loadImages();
		setLayout(null);
		displayedImage = displayableImages[0];
		
		addMouseListener(new MouseInput());

		backButton = new JButton();
		backButton.setLocation(15, 360);
		backButton.setSize(58, 28);
		backButton.addActionListener(new ButtonInput());
		backButton.setContentAreaFilled(false);
		backButton.setBorder(null);
		add(backButton);
		nextButton = new JButton();
		nextButton.setLocation(125, 361);
		nextButton.setSize(56, 27);
		nextButton.addActionListener(new ButtonInput());
		nextButton.setContentAreaFilled(false);
		nextButton.setBorder(null);
		add(nextButton);

		setVisible(true);
	}

	public void loadImages()
	{
		int currentIndex = 0;
		MediaTracker mt = new MediaTracker(this);
		Toolkit tk = this.getToolkit();
		for(int i = 0; i < displayableImages.length; i++)
		{
			int j = i + 1;
			displayableImages[i] = tk.getImage("resources/instructionsPanel" + j + ".png");
			mt.addImage(displayableImages[i], currentIndex);
			currentIndex++;
		}
		bgImage = tk.getImage("resources/instructionsBgImage.png");
		mt.addImage(bgImage, currentIndex);
		currentIndex++;
		backImage = tk.getImage("resources/instructionsBackImage.png");
		mt.addImage(backImage, currentIndex);
		currentIndex++;
		nextImage = tk.getImage("resources/instructionsNextImage.png");
		mt.addImage(nextImage, currentIndex);
		currentIndex++;

		try
		{
			mt.waitForAll();
		}
		catch(InterruptedException e) {}

	}

	public void paint(Graphics g)
	{
		if(offGraphics == null || getWidth() != offImage.getWidth(null) || getHeight() != offImage.getHeight(null))
		{
			offImage = createImage(getWidth(), getHeight());
			offGraphics = offImage.getGraphics();
		}

		Graphics2D g2D = (Graphics2D) offGraphics;
		g2D.setBackground(new Color(0, 0, 0, 0));
		g2D.clearRect(0, 0, 500, 500);
		offGraphics.setColor(new Color(255, 235, 245, 0));
		offGraphics.fillRect(0, 0, 200, 400);
		offGraphics.drawImage(bgImage, 0, 0, this);
		offGraphics.drawImage(displayedImage, 0, -10, this);
		offGraphics.drawImage(backImage, 15, 360, this);
		offGraphics.drawImage(nextImage, 125, 361, this);
		if(progressCount == 3 && !powerUpSelected)
			offGraphics.drawImage(Display.powerUps[0], 110, 320, this);
		if(progressCount == 3 && powerUpSelected)
			offGraphics.drawImage(Display.powerUpsSelected[0], 110, 320, this);
		g.drawImage(offImage, 0, 0, this);
	}

	public void step(int theDirection)
	{
		if(theDirection > 0)
			progressCount++;
		if(theDirection < 0)
			progressCount--;
		displayedImage = displayableImages[progressCount];
		repaint();
	}

	private class MouseInput extends MouseAdapter implements MouseMotionListener
	{
		private Point cursorPoint = new Point();

		public MouseInput()
		{

		}

		public void mouseDragged(MouseEvent e) {}

		public void mouseMoved(MouseEvent e)
		{
			cursorPoint = e.getPoint();
		}

		public void mousePressed(MouseEvent e)
		{
			cursorPoint = e.getPoint();
			_Engine.gameScreen.cursorPoint = new Point();
			if(cursorPoint.x - 110 + _Engine._Display.getLocation().x <= 40 && 
					cursorPoint.x - 110 + _Engine._Display.getLocation().x >= 0 && 
					cursorPoint.y - 320 + _Engine._Display.getLocation().y <= 40 && 
					cursorPoint.y - 320 + _Engine._Display.getLocation().y >= 0)
			{
				powerUpSelected = true;
				_Engine.gameScreen.draggedPowerUpImage = Display.powerUps[0];
			}
		}
	}

	private class ButtonInput implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			if(e.getSource() == backButton)
			{
				if(progressCount == 0)
					new MainMenu(_Engine._Display);
				else
				{
					step(-1);
					((InstructionsEngine) _Engine).startLevel(progressCount + 1);
				}
			}
			if(e.getSource() == nextButton)
			{
				if(progressCount == 5)
					new MainMenu(_Engine._Display);
				else
				{
					step(1);
					((InstructionsEngine) _Engine).startLevel(progressCount + 1);
				}
			}
		}
	}
}
